package common;

import basic.J2CodeSessionLoginBasic;
import basic.QrCodeBasic;

/**
 * 微信 Api
 *
 * @author gzkemays
 * @since 2021/12/13 17:11
 */
public class WxApi {
  public static final String J2CODE_SESSION_LOGIN =
      "https://api.weixin.qq.com/sns/jscode2session?appid="
          + WxPattern.APPID
          + "&secret="
          + WxPattern.SECRET
          + "&js_code="
          + WxPattern.JSCODE
          + "&grant_type=authorization_code";
  public static final String ACCESS_TOKEN =
      "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="
          + WxPattern.APPID
          + "&secret="
          + WxPattern.SECRET;
  public static final String QRCODE_HAS_LIMIT =
      "https://api.weixin.qq.com/wxa/getwxacode?access_token=" + WxPattern.API_ACCESS_TOKEN;
  public static final String QRCODE_UN_LIMIT =
      "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + WxPattern.API_ACCESS_TOKEN;
  public static final String QRCODE_NON_QUERY =
      "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token="
          + WxPattern.API_ACCESS_TOKEN;

  /**
   * 获取登录 API
   *
   * @param basic J2Code基本信息
   * @return 替换的请求地址
   */
  public static String j2SessionLoginApi(J2CodeSessionLoginBasic basic) {
    return J2CODE_SESSION_LOGIN
        .replace(WxPattern.APPID, basic.getAppid())
        .replace(WxPattern.SECRET, basic.getSecret())
        .replace(WxPattern.JSCODE, basic.getJscode());
  }

  /**
   * Access api
   *
   * @param baseMsg 基本信息 appid 和 secret
   * @return 替换的请求地址
   */
  public static String accessTokenApi(WxBaseMsg baseMsg) {
    return ACCESS_TOKEN
        .replace(WxPattern.APPID, baseMsg.appid)
        .replace(WxPattern.SECRET, baseMsg.secret);
  }

  /**
   * 获取指定的二维码
   *
   * @param sw 获取参数集合
   * @return 二维码生成地址
   */
  public static String qrcode(QrCodeBasic sw) {
    String accessToken = sw.getAccessToken();
    Integer mode = sw.getMode();
    if (mode.equals(QrCodeMode.API_HAS_LIMIT)) {
      return QRCODE_HAS_LIMIT.replace(WxPattern.API_ACCESS_TOKEN, accessToken);
    } else if (mode.equals(QrCodeMode.API_UN_LIMIT)) {
      return QRCODE_UN_LIMIT.replace(WxPattern.API_ACCESS_TOKEN, accessToken);
    } else if (mode.equals(QrCodeMode.API_NON_QUERY)) {
      return QRCODE_NON_QUERY.replace(WxPattern.API_ACCESS_TOKEN, accessToken);
    }
    throw new IllegalArgumentException("没有指定模式");
  }
}
